package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.entity.Custom;
import pers.cj.pdos.entity.Provider;
import pers.cj.pdos.entity.ProviderType;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.IProviderService;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.CountVo;
import pers.cj.pdos.vo.ProviderVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-12-12
 */
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements IProviderService {
    @Autowired
    ProviderMapper providerMapper;
    @Autowired
    ProviderTypeMapper providerTypeMapper;
    @Autowired
    OrderRuleMapper orderRuleMapper;
    @Override
    public boolean checkName(String companyId, String name) {
        return providerMapper.selectCount(new QueryWrapper<Provider>().eq("company_id",companyId).eq("name",name)) > 0;
    }

    @Override
    public boolean checkNo(String companyId, String no) {
        return providerMapper.selectCount(new QueryWrapper<Provider>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public List<ProviderVo> listVoForInit(String companyId, Integer page, Integer limit, String search) {
        return providerMapper.listVoForInit(companyId, Utils.genPage(page,limit), limit, search);
    }

    @Override
    public Long countForInit(String companyId, String search) {
        return providerMapper.countForInit(companyId, search);
    }

    @Override
    public List<ProviderVo> listVoPage(String companyId,
                                       Integer page,
                                       Integer limit,
                                       Integer status,
                                       String search,
                                       String providerTypeId,
                                       String no,
                                       String name,
                                       String unitTel,
                                       String phone,
                                       String contacts,
                                       String remark,
                                       String storeId){
        return providerMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                status,
                search,
                providerTypeId,
                no,
                name,
                unitTel,
                phone,
                contacts,
                remark,
                storeId);
    }

    @Override
    public long count(String companyId,
                      Integer status,
                      String search,
                      String providerTypeId,
                      String no,
                      String name,
                      String unitTel,
                      String phone,
                      String contacts,
                      String remark,
                      String storeId){
        return providerMapper.count(companyId,
                status,
                search,
                providerTypeId,
                no,
                name,
                unitTel,
                phone,
                contacts,
                remark,
                storeId);
    }

    @Override
    public void update(Provider provider) {
        if (provider == null) {
            return;
        }

        if (provider.getStatus()==2){
            //停用的话，则取消默认设置
            provider.setDef(false);
        }

        if (provider.getDef()) {
            //如果该客户是默认的，那么其他的默认客户要改成非默认
            Provider noDef = new Provider();
            noDef.setDef(false);
            providerMapper.update(noDef, new QueryWrapper<Provider>().eq("company_id", provider.getCompanyId()).eq("def",true));
        }
        providerMapper.updateById(provider);
    }

    @Override
    public void batchChangeType(String providerTypeId, List<Provider> list) {
        providerMapper.batchChangeType(providerTypeId,list);
    }

    @Override
    public Map<String, Long> check(List<Provider> providerList) {
        List<CountVo> countVoList = providerMapper.checkList(providerList);
        Map<String,Long> map = new HashMap<>();
        for (CountVo countVo : countVoList) {
            if (!countVo.getId().equals("0") ) {
                Long count = map.get(countVo.getId())==null ? Long.valueOf(0) : map.get(countVo.getId());
                count += countVo.getCount();
                map.put(countVo.getId(), count);
            }
        }
        return map;
    }

    @Override
    public void deleteBatch(List<Provider> list) {
        providerMapper.deleteBatch(list);
    }

    @Override
    public void insert(List<Provider> providerList, List<ProviderType> providerTypeList) {
        if (!Utils.isEmpty(providerTypeList)){
            providerTypeMapper.insertBatch(providerTypeList);
        }

        providerMapper.insertBatch(providerList);
    }

    @Override
    public List<ProviderVo> listVo(String companyId) {
        return providerMapper.listVo(companyId,null,null, null,null,null, null,null,null,null,null,null,null);
    }

    @Override
    public List<ProviderVo> listVoByIds(List<String> ids) {
        return providerMapper.listVoByIds(ids);
    }

    @Override
    public Provider getDefault(String storeId) {
        List<Provider> list = providerMapper.selectList(new QueryWrapper<Provider>().eq("status","1").eq("def",1).eq("store_id",storeId));
        if (Utils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean save(Provider provider) {
        if (provider == null) {
            return false;
        }

        if (provider.getStatus()==2){
            //停用的话，则取消默认设置
            provider.setDef(false);
        }
        if (provider.getDef()) {
            //如果该客户是默认的，那么其他的默认客户要改成非默认
            Provider noDef = new Provider();
            noDef.setDef(false);
            providerMapper.update(noDef, new QueryWrapper<Provider>().eq("company_id", provider.getCompanyId()).eq("def",true));
        }

        //客户编号+1
//        orderRuleMapper.addOrderRuleCurrent(provider.getCompanyId(), OrderType.GYS.getType());
        return super.save(provider);
    }
}
